//
//  ImageScrollView.m
//  MeiXiu
//
//  Created by Yuan on 12-12-25.
//
//

#import "ImageScrollView.h"
#import "UIImageView+WebCache.h"

@implementation ImageScrollView
@synthesize imageView = _imageView;

- (id)initWithFrame:(CGRect)frame url:(NSString *)url target:(id)target
{
    if ((self = [super initWithFrame:frame]))
	{
		self.delegate = self;
		self.minimumZoomScale = 0.5;
		self.maximumZoomScale = 2.5;
		self.showsVerticalScrollIndicator = NO;
		self.showsHorizontalScrollIndicator = NO;
		
        UITapGestureRecognizer *singleTapOneFinger = [[UITapGestureRecognizer alloc]initWithTarget:target action:@selector(handleSingleTap:)];
        singleTapOneFinger.numberOfTapsRequired = 1;
        singleTapOneFinger.numberOfTouchesRequired = 1;
        singleTapOneFinger.delegate = target;
        [self addGestureRecognizer:singleTapOneFinger];
        
		_imageView  = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self resizeImageView:nil withUrl:url];
		[self addSubview:_imageView];
    }
    return self;
}

-(void)resizeImageView:(UIImage*)img withUrl:(NSString*)url
{
//    __block typeof (self) bself = self;
   
    __block id myself = self;
    
    if (url&&[url length]>0)
    {
        [_imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"loaddefault"] completed:^(UIImage *img,NSError *error,SDImageCacheType type){
            [myself resizeImageView:img withUrl:nil];
        }];
    }
    else
    {
        [_imageView setImage:img];
    }
    _imageView.alpha = 0.05f;
    
    CGRect rect = _imageView.frame;
    CGSize fitsize = [self getFitSizeFromCGSize:_imageView.image.size withMaxWidth:self.frame.size.width    withMaxHeight:460.0];
    //计算图片中心点的坐标
    CGFloat offsetX = (self.bounds.size.width > self.contentSize.width)?
    (self.bounds.size.width - self.contentSize.width) * 0.5 : 0.0;
    CGFloat offsetY = (self.bounds.size.height > self.contentSize.height)?
    (self.bounds.size.height - self.contentSize.height) * 0.5 : 0.0;
    CGPoint centerPoint = CGPointMake(self.contentSize.width * 0.5 + offsetX,
                                      self.contentSize.height * 0.5 + offsetY);
    rect.size.width = fitsize.width;
    rect.size.height = fitsize.height;
    rect.origin.x = centerPoint.x - rect.size.width/2;
    rect.origin.y = centerPoint.y - rect.size.height/2;
    _imageView.frame = rect;
    
    [UIView animateWithDuration:0.3 animations:^(void){
        _imageView.alpha = 1.0;
    }];
    
    [self setZoomScale:1.001f];
}


-(CGSize)getFitSizeFromCGSize:(CGSize)fitSize withMaxWidth:(CGFloat)w withMaxHeight:(CGFloat)h
{
    float _originalWidth = fitSize.width;
    float _originalHeight = fitSize.height;
    float _resultWidth = 0.0f;
    float _resultHeight = 0.0f;
    if (_originalWidth <= w && _originalHeight <=h) {
        _resultWidth = _originalWidth;
        _resultHeight = _originalHeight;
    }
    else
    {
        if (_originalWidth/_originalHeight > w/h ) {
            if (_originalWidth >= w) {
                _resultWidth = w;
                _resultHeight = _resultWidth*_originalHeight/_originalWidth;
            }
            else
            {
                _resultWidth = _originalWidth;
                _resultHeight = _resultWidth*_originalHeight/_originalWidth;
            }
        }
        else
        {
            if (_originalHeight >= h) {
                _resultHeight = h;
                _resultWidth = _resultHeight*_originalWidth/_originalHeight;
            }
            else
            {
                _resultHeight = _originalHeight;
                _resultWidth = _resultHeight*_originalWidth/_originalHeight;
            }
        }
    }
    return CGSizeMake(_resultWidth, _resultHeight);
}



#pragma mark -
#pragma mark === UIScrollView Delegate ===
#pragma mark -
//让图片居中
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
    (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
    (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
    _imageView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
                                 scrollView.contentSize.height * 0.5 + offsetY);
}

#pragma UIScrollViewDelegate Methord
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView_
{
    return _imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{	
//	CGFloat zs = scrollView.zoomScale;
//	zs = MAX(zs, 1.0);
//	zs = MIN(zs, 2.0);	
//	[UIView beginAnimations:nil context:NULL];
//	[UIView setAnimationDuration:0.3];
//	scrollView.zoomScale = zs;
//	[UIView commitAnimations];
}

#pragma mark -
#pragma mark === dealloc ===
#pragma mark -
- (void)dealloc
{
    _imageView = nil;
}


@end
